﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using MyX.DSL;
using MyX.Core;
using LearningEnglish.Models;
using LearningEnglish.ViewModels;
using LearningEnglish.ThridAPI;
using System.IO;

namespace LearningEnglish.LogicDSL
{
    /// <summary>
    /// 详细说明了单词书中各种内容的创建及如何获取的逻辑
    /// </summary>
    public class WordBookContentDSL : XIWorkDSL
    {
        public void Init()
        {
            var work = new WorkHelper();

            work.Command<CreateWordBook>(
                c =>
                {
                    var book = new WordBook(c.CreateUserID, c.Name, c.Message, c.ImgID);

                    c.SetResult(book.ID);
                });

            work.Event<WordBookCreated>(
                e =>
                {
                    var rd = new WordBookRD() { ID = e.XSenderID };

                    var mapper = new XMapper(e, rd);
                    mapper.Map();

                    rd.ImgPath = FileService.Instance.GetVirtualPath(Guid.Parse(rd.ImgID));

                    VM.WordBooks.Add(rd);

                }, true);

            work.Command<AddWordIntoWordBook>(
                c =>
                {
                    //当该单词在字典中不存在的时，则进行翻译获取和读音获取
                    if (EnglishDictionary.Instance.Translate(c.Word) == null)
                    {
                        var command = new TranslateWord() { Word = c.Word };
                        command.Send();
                        var status = command.Status().Result;
                        if (status.Status != XSimpleStatus.MessageStatus.Ok)
                            throw new Exception(status.Result);
                    }
                    var wordBook = new WordBook(c.XSenderID);
                    wordBook.Add(c.Word);
                });
            //获取单词意义和下载读音文件
            work.Command<TranslateWord>(
                c =>
                {
                    var localFile = DownYoudaoMp3.Down(c.Word);
                    FileService.Instance.AddLocalFile(localFile);
                    var fileID = FileService.Instance.GetFileID(Path.GetFileName(localFile));
                    EnglishDictionary.Instance.Add(c.Word, "因为不爽api限制，所以暂不提供解释", FileService.Instance.GetVirtualPath(fileID));
                    c.SetResult(EnglishDictionary.Instance.Translate(c.Word));
                });

            //将单词添加到单词书的读模型中
            work.Event<WordBookWordAdded>(
                e =>
                {
                    var rd = (from p in VM.WordBooks where p.ID == e.XSenderID select p).Single();
                    var mean = EnglishDictionary.Instance.Translate(e.Word);
                    rd.Words.Add(new WordBookRDItem() { Word = e.Word, Mean = mean.Item1, PronunciationFile = mean.Item2 });
                }, true);

            work.Event<WordBookWordAdded>(
                e =>
                {
                    var planIDs = (from p in VM.LearnPlans where p.WordBook.ID == e.XSenderID select p.ID).ToList();
                    foreach (var id in planIDs)
                    {
                        var plan = new LearnPlan(id);
                        plan.Add(e.Word);
                    }
                });
            work.Event<WordBookWordRemoved>(
                e =>
                {
                    var planIDs = (from p in VM.LearnPlans where p.WordBook.ID == e.XSenderID select p.ID).ToList();
                    foreach (var id in planIDs)
                    {
                        var plan = new LearnPlan(id);
                        plan.Remove(e.Word);
                    }
                });
        }
    }

    public class CreateWordBook : XCommand
    {
        public string Name { get; set; }
        public string Message { get; set; }
        public Guid ImgID { get; set; }

        public Guid CreateUserID { get; set; }
    }

    public class AddWordIntoWordBook : XCommand
    {
        public string Word { get; set; }
    }

    public class TranslateWord : XCommand
    {
        public string Word { get; set; }
    }
}